<?php
 require_once(PATCH_DRIVERS_DISPLAY_SMARTY."Smarty.class.php");
 
 class __driver_display extends Smarty {
 	
    protected $theme = "default";
    static $change_theme = "";
    
    protected $DefaultIndexTemplate = "index.html";
    static $change_default_index_template = "";
	
    protected $DefaultBlockTemplate = "block.html";
	static $change_default_block_template = "";
    
	/**
	 * Массив, в котором описываются функции Smarty.
	 * Ключ - название функции в Smarty
	 * Значение - название реальной функции PHP
	 */
	private static $smarty_functions = array(
		"elang" => "glang_smarty"	// Функция принадлежит драйверу LANG
	);
	
 	/**
 	 * Патч к Smarty позволяющий использовать в качестве template folder
 	 * папку самого модуля и позволяет использовать темы
 	 *
 	 * @param array/string $resource_name
 	 * @param int $cache_id
 	 * @param int $compile_id
 	 * @param bool $display
 	 * @return string
 	 */
    function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) {
                
    	if(is_array($resource_name)) {
        	
                if($resource_name[1]->get() == "") {
		    	    return __kernel_error::e404();
		        } else {
		            $this -> template_dir = "modules/".$resource_name[1]->get()."/templates/";
		            
		            if(is_file('modules/'.$resource_name[1]->get().'/templates/'.$this->theme.'/'.$resource_name[0])) {
		                $this -> template_dir = "modules/".$resource_name[1]->get().'/templates/'.$this->theme;
		            } else { $this -> template_dir = "modules/".$resource_name[1]->get().'/templates/default'; }
		        	
		            if(is_file($this -> template_dir.'/'.$resource_name[0]))
		            	$fetch = parent::fetch($resource_name[0], $cache_id, $compile_id, $display);
		            else $fetch = $this->error_templateNotFound($this -> template_dir.'/'.$resource_name[0]);
		            
		            $this -> template_dir = "themes/".$this->theme;
		                            
		            return $fetch;
		        }
	                
        } else {
        	if(is_file($this -> template_dir.'/'.$resource_name))
		    	return parent::fetch($resource_name, $cache_id, $compile_id, $display);
		    else return $this->error_templateNotFound($this -> template_dir.'/'.$resource_name); parent::fetch($resource_name, $cache_id, $compile_id, $display);
        }
        
    }
    
    /**
     * Перехват ошибок Smarty
     * 
     * @see htdocs/kernel/smarty/Smarty#trigger_error($error_msg, $error_type)
     */
    function trigger_error($error_msg, $error_type = E_USER_WARNING)
    {
        trigger_error("drv(DISP) error: $error_msg", $error_type);
    }
    
    function error_templateNotFound($path, $return = true) {
    	
    	$error_msg = "Template <b>$path</b> not found";
    	
    	if($return === true) return $error_msg;
    	else echo $error_msg;
    }
    
    
    public function getTheme() {
    	return $this->theme;
    }
    
    public function getIndexTemplate() {
    	return $this->DefaultIndexTemplate;
    }
    
 	public function getBlockTemplate() {
    	return $this->DefaultBlockTemplate;
    }
    // Hold an instance of the class
    private static $instance;
    
    // A private constructor; prevents direct creation of object
    private function __construct() 
    {
        if(self::$change_theme != "") $this->theme = self::$change_theme;
        
        if(self::$change_default_index_template != "") $this->DefaultIndexTemplate = self::$change_default_index_template;
        if(self::$change_default_block_template != "") $this->DefaultBlockTemplate = self::$change_default_block_template;
        
        $this -> template_dir = "themes/".$this->theme;
        
        
        $this->register_function('elang', 'glang_smarty'); // Register glang pligin for Smarty
        $this->register_block('url2href', url2href);
        
    }

    public function changeTemplates($index=false, $block=false) {
    	if($index != false) $this->DefaultIndexTemplate = $index;
        if($block != false) $this->DefaultBlockTemplate = $block;
    }
    
    // The singleton method
    public static function si() 
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }

    // Prevent users to clone the instance
    public function __clone()
    {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
 
 }

?>
